$(function(){
	$("#sResult").append(loading);
	var url = $.query.get("url");
	var book = $.query.get("book");
	if(url!=""){
		$("#btnAddFav").hide();
		$.get(uri+url,function(html){
			callback(html);
		});
	}
	if(book!=""){
		$("#kw").val(book);
		$.get(uri+"search.php?w="+book,function(html){
			callback(html);
		});
	}
	
	var callback = function(html){
		$("#sResult").empty();
		var tbs = $(html).find("#Tbs:first");
		tbs.find("a").each(function(){
			var href = $(this).attr("href");
			if(href.indexOf("/kz/rd.php")>-1){
				href = href.replace(/\/kz\/rd\.php\?url=/,"");
				$(this).attr("href",href);
			}else if(href.indexOf("/kz/kz.php")>-1){
				$(this).attr("href","reader.html?url="+encodeURIComponent(href))
			}else if(href.indexOf("/search.php")>-1){
				$(this).attr("href","search.html?url="+encodeURIComponent(href))
			}
		});
		$("#sResult").append(tbs);	
	}
	
	//btnAddFav
	$("#btnAddFav").click(function(){
		var favStr = localStorage.getItem("favBookInfo") || "[]";
		var favJson = JSON.parse(favStr);
		if($.inArray(book,favJson)<0){
			favJson.unshift(book);
		}	
		localStorage.setItem("favBookInfo",JSON.stringify(favJson));
		alert("\u60A8\u597D\uFF0C["+book+"] \u5DF2\u6210\u529F\u52A0\u5165\uFF0C\u5373\u5C06\u524D\u5F80\u6211\u7684\u4E66\u67B6\uFF01");
		location.href="favorites.html";
	});
})